# 
# Description:
# This script constructs figures A1 and A2
# for Appendix A
#
# Requires: terror.dta, terror_new.csv, motives.xlsx
#
# Author: Sean Nossek
#
# Date: August 04, 2020

# load packages

if (!require("pacman")) install.packages("pacman")

pacman::p_load(ggplot2, tidyverse, readstata13, readxl, maps, sf, tmap, xtable)

# set dir 
setwd("C:/Users/Sean/Dropbox/Eran/Terrorism_Left/Paper/Revisions/Revised Paper/Replication/Paper Data")


# read terror event data and merge with manually coded motives
terror_base <- read.dta13("terror.dta")
motives <- read_excel("motives.xlsx")
terror_new <- read_csv("terror_new.csv")
terror_base <- rbind(terror_base, terror_new)
terror <- merge(terror_base, motives, by = "eventid", all.x = TRUE) %>%
  rename(fips = FIPSCounty)


terror <- terror %>% 
  group_by(fips) %>% 
  mutate(attempts = n()) %>% 
  group_by(fips, attempts) %>%
  summarise(success = sum(success, na.rm = TRUE))


# Load the county data from the maps package
cnty <- st_as_sf(map("county", plot = FALSE, fill = TRUE))

data("county.fips")

cnty2 <- cnty %>%
  left_join(county.fips, by=c("ID" = "polyname"))

cnty2.df1 <- left_join(cnty2, terror, by="fips") %>%
  mutate(success = ifelse(is.na(success), 0, success),
         attempts = ifelse(is.na(attempts), 0, attempts),
         any = as.numeric(success > 0),
         success_cat = ifelse(success > 20, "21+", ifelse(success <= 20 & success > 15, "16-20", ifelse(success <= 15 & success > 10, "11-15", ifelse(success > 5 & success <= 10, "6-10", ifelse(success > 0 & success <= 5, "1-5", "0"))))),
         attempts_cat = ifelse(attempts > 20, "21+", ifelse(attempts <= 20 & attempts > 15, "16-20", ifelse(attempts <= 15 & attempts > 10, "11-15", ifelse(attempts > 5 & attempts <= 10, "6-10", ifelse(attempts > 0 & attempts <= 5, "1-5", "0"))))))

cnty2.df1 <- cnty2.df1 %>%
  mutate(success_cat = factor(success_cat, order = TRUE, levels = c("0","1-5","6-10","11-15","16-20","21+")),
         attempts_cat = factor(attempts_cat, order = TRUE, levels = c("0","1-5","6-10","11-15","16-20","21+")))


# load county statistics. install package from archive.

packageurl <- "https://cran.r-project.org/src/contrib/Archive/noncensus/noncensus_0.1.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(noncensus)


data("counties") 

county_pop <- counties %>%
  mutate(fips = paste0(state_fips, county_fips))

cnty2.df1$fips <- str_pad(cnty2.df1$fips, 5, pad = "0")

# make statistics for maps

cnty2.df1 <- merge(cnty2.df1, county_pop, by = "fips", all.x = TRUE) %>%
  mutate(success_pc = (success / population) * 100000,
         attempts_pc = attempts / population)

cnty2.df1 <- cnty2.df1 %>%
  mutate(success_pc = ifelse(is.na(success_pc), 0, success_pc),
         success_pc_cat = ifelse(success_pc > 100, "101+", ifelse(success_pc > 60 & success_pc <= 100, "61-100", ifelse(success_pc > 35 & success_pc <= 60, "36-60", ifelse(success_pc > 20 & success_pc <= 35, "21-35", ifelse(success_pc <= 20 & success_pc > 15, "16-20", ifelse(success_pc <= 15 & success_pc > 10, "11-15", ifelse(success_pc > 5 & success_pc <= 10, "6-10", ifelse(success_pc > 0 & success_pc <= 5, "1-5", "0")))))))))

cnty2.df1 <- cnty2.df1 %>%
  mutate(success_pc_cat = factor(success_pc_cat, order = TRUE, levels = c("0","1-5","6-10","11-15","16-20","21-35","36-60","61-100","101+")))

cnty2.df1 <- cnty2.df1 %>%
  mutate(failures = attempts - success, failures_cat = ifelse(failures > 20, "21+", ifelse(failures <= 20 & failures > 15, "16-20", ifelse(failures <= 15 & failures > 10, "11-15", ifelse(failures > 5 & failures <= 10, "6-10", ifelse(failures > 0 & failures <= 5, "1-5", "0"))))),
         failures_cat = factor(failures_cat, order = TRUE, levels = c("0","1-5","6-10","11-15","16-20","21+")))

# map

attempts_map <- tm_shape(cnty2.df1) +
  tm_fill(col = "attempts_cat", palette = "YlOrRd", title = "Attack Count") +
  tm_layout(panel.labels = "Distribution of Attempted Attacks in Mainland U.S.")

success_map <- tm_shape(cnty2.df1) +
  tm_fill(col = "success_cat", palette = "YlOrRd", title = "Successful Attacks") +
  tm_layout(panel.labels = "Distribution of Successful Attacks in Mainland U.S.") 

fail_map <- tm_shape(cnty2.df1) +
  tm_fill(col = "failures_cat", palette = "YlOrRd", title = "Failed Attacks") +
  tm_layout(panel.labels = "Distribution of Failed Attacks in Mainland U.S.")

both_maps <- tmap_arrange(success_map, fail_map)

tmap_save(both_maps,"terror_maps.png", width = 8, height = 9, dpi = 400)

rm(list = ls())

# Figure A2 - over time

# load data
pres_wide <- read.dta13("terror_pres_wide.dta")


# make summary stats
temp <- pres_wide %>%
  group_by(year) %>%
  summarize(success_count = sum(success),
            failed_count = sum(success == 0))

#plot
plottt <- ggplot(temp) +
  geom_line(aes(year, success_count, color = "Successful"), size = 1.5) + 
  geom_line(aes(year, failed_count, color = "Failed"), size = 1.5) +
  theme(panel.grid.minor = element_blank()) + 
  labs(title="Successful and Failed Attacks Over Time", x = "Year",y="Count",color=NULL) +
  scale_color_manual(name = "Attacks", values = c("Successful" = "#00ba38", "Failed" = "#f8766d"))

ggsave("year_line.png", dpi = 300)




